# Name:    CGenerateDb.py
# Date:    Sat May  1 20:37:18 CEST 2010
# Purpose: Generate DB file.
# Depends: CThread, CFile
# Def:     CGenerateDB(dataHandler)
# Inputs:  dataHandler - instance of CDataHnd

import sys
import gtk

# my libraries
from CThread import CThread
from CFile import CFile
from CProcMngr import CProcMngrStatusException
from CExceptionHnd import CExceptionHnd
import myconst

import time

####################################################################################################

class CGenerateDb(CThread, CExceptionHnd):
	'''
	'' Generate DB file
	'''
	__NAME='CGenerateDb Thread'
	
	__oDataHnd = None
	__progBarWnd = None

#---------------------------------------------------------------------------------------------------

	def __init__(self, dataHandler):
		super(CGenerateDb, self).__init__(self.__NAME)
		self.__oDataHnd = dataHandler

#---------------------------------------------------------------------------------------------------

	def run(self):
		try:

			findCmd = self.__buildFindCmd()
			self.__oDataHnd.getProcMngr().setCmd(findCmd)

			(status, output) = self.__oDataHnd.getProcMngr().getProcStatusOutput()
			try:
				self.__oDataHnd.oDBFile = CFile(self.__oDataHnd.getDbFilename(), 'w')
				self.__oDataHnd.oDBFile.getFileDesc().write(output + '\n')
			except IOError, e:
				self.handle()
		except CProcMngrStatusException, e:
			self.setMsg(myconst.MSG_FILEGENERATED_FAILED + str(e.getStatus()))
			self.handle()
		except IOError, e:
			self.handle()
		finally:
			super(CGenerateDb, self).run()

#---------------------------------------------------------------------------------------------------

	def __buildFindCmd(self):
		followSymLink = str(self.__oDataHnd.getSymLink())
		searchPath = self.__oDataHnd.getSearchPath()

		if followSymLink in '1' or followSymLink in 'Yes' or followSymLink in 'True':
			cmd = 'find -L '
		else:
			cmd = 'find '

		cmd += searchPath

		prependOr = False
		for ext in self.__oDataHnd.getExt():
			if prependOr is True:
				cmd += ' -or'

			cmd += ' -iname \'*' + ext + '\''

			if prependOr is False:
				prependOr = True

		return cmd


####################################################################################################
